#! /bin/sh
#
# chkconfig: - 85 15
# description: zabbix java gateway
#

### BEGIN INIT INFO
# Provides: zabbix
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: start and stop zabbix java gateway
# Description: Zabbix Java Gateway
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Source Zabbix Java Gateway configuration
. /etc/zabbix/zabbix_java_gateway.conf

if [ -r "/etc/sysconfig/zabbix-java-gateway" ]; then
    . /etc/sysconfig/zabbix-java-gateway
fi

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0
EXECDIR=/usr/sbin/zabbix_java

case "$1" in
    start)
        echo -n "Starting zabbix java gateway: "
        if [ -n "$PID_FILE" -a -e "$PID_FILE" ]; then
            echo "zabbix java gateway is already running"
            exit 1
        fi

        JAVA=${JAVA:-java}

        JAVA_OPTIONS="-server $JAVA_OPTIONS"
        if [ -z "$PID_FILE" ]; then
            JAVA_OPTIONS="$JAVA_OPTIONS -Dlogback.configurationFile=logback-console.xml"
        fi

        cd $EXECDIR

        CLASSPATH="lib"
        for jar in {lib,bin}/*.jar; do
            if [[ $jar != *junit* ]]; then
                CLASSPATH="$CLASSPATH:$jar"
            fi
        done

        if [ -n "$PID_FILE" ]; then
            ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.pidFile=$PID_FILE"
        fi
        if [ -n "$LISTEN_IP" ]; then
            ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.listenIP=$LISTEN_IP"
        fi
        if [ -n "$LISTEN_PORT" ]; then
            ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.listenPort=$LISTEN_PORT"
        fi
        if [ -n "$START_POLLERS" ]; then
            ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.startPollers=$START_POLLERS"
        fi
        if [ -n "$TIMEOUT" ]; then
            ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dzabbix.timeout=$TIMEOUT"
        fi

        tcp_timeout=${TIMEOUT:=3}000
        ZABBIX_OPTIONS="$ZABBIX_OPTIONS -Dsun.rmi.transport.tcp.responseTimeout=$tcp_timeout"

        COMMAND_LINE="$JAVA $JAVA_OPTIONS -classpath $CLASSPATH $ZABBIX_OPTIONS com.zabbix.gateway.JavaGateway"

        if [ -n "$PID_FILE" ]; then
            PID=$(su -s /bin/bash -c "$COMMAND_LINE > /dev/null 2>&1 & echo \$!" zabbix)
            if ps -p $PID > /dev/null 2>&1; then
                echo $PID > $PID_FILE
            else
                echo "zabbix java gateway did not start"
                exit 1
            fi
        else
            exec $COMMAND_LINE
            RETVAL=$?
        fi
        ;;
    stop)
        echo -n "Shutting down zabbix java gateway: "
        if [ -n "$PID_FILE" ]; then
            if [ -e "$PID_FILE" ]; then
                    kill `cat $PID_FILE` && rm $PID_FILE
            else
                echo "zabbix java gateway is not running"
                exit 1
            fi
        else
            echo "zabbix java gateway is not configured as a daemon: variable \$PID_FILE is not set"
            exit 1
        fi
        ;;
    restart)
        $0 stop
        $0 start
        RETVAL=$?
        ;;
    condrestart)
        if [ -n "$PID_FILE" -a -e "$PID_FILE" ]; then
            $0 stop
            $0 start
        fi
        RETVAL=$?
        ;;
    status)
        status -p $PID_FILE $0
        RETVAL=$?
        exit $RETVAL
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
        ;;
esac

[ "$RETVAL" -eq 0 ] && success $"$base startup" || failure $"$base startup"
echo
exit $RETVAL

